package com.itboye.mengwu.app.login.ui;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.logincontroller.LoginController;
import com.itboye.mengwu.app.logincontroller.LoginedState;
import com.itboye.mengwu.app.ui.main.MainPresenter;
import com.itboye.mengwu.data.model.PersonDataBean;
import com.itboye.mengwu.data.presenter.GetYanzhengMaPresenter;

public class SmsLoginActivity extends BaseActivity implements OnClickListener,
		Observer {

	private Button loginbutton;
	private Button yzmbutton;
	private EditText phoneedit;
	private EditText yzmedit;
	private TimeCount time;
	private String username;
	private RelativeLayout backrela;
	protected String code;
	GetYanzhengMaPresenter getYanzhengMaPresenter;
	MainPresenter mainPresenter;
	public void initData() {
		getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
		mainPresenter = new MainPresenter(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.yzmbutton:
			username = phoneedit.getText().toString();
			if (!username.equals("")) {
				getyzm();
				time.start();
			} else {
				Toast.makeText(SmsLoginActivity.this, "请输入手机号", 0).show();
			}
			break;
		case R.id.loginbutton:
			code=yzmedit.getText().toString().trim();
			if (code == null) {
				Toast.makeText(this, "请输入验证码", 0).show();
				return;
			}
			login();
			break;
		case R.id.backrela:
			this.finish();
			break;

		default:
			break;
		}

	}

	public void getyzm() {
		getYanzhengMaPresenter.getVerificationCode(username, 5 + "");
	}

	public void login()

	{
		mainPresenter.login(username, code, 1);
	}

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_yzm_login;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		time = new TimeCount(60000, 1000, yzmbutton);
		username = phoneedit.getText().toString();
		initData();
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaSuccessEvent) {
				code=entity.getData() + "";
				yzmedit.setText(entity.getData() + "");
			} else if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaFailEvent) {
				ByAlert.alert(entity.getData() + "");
			}else if(entity.getEventType() == MainPresenter.LoginSuccessEvent){
				LoginController.setLoginState(new LoginedState());
				PersonDataBean bean=(PersonDataBean)entity.getData();
				bean.setUid(bean.getUid());
				bean.setMobile(bean.getMobile());
				bean.setHead(bean.getHead());
				bean.setDefault_address(bean.getDefault_address());
				bean.setNickname(bean.getNickname());
				bean.setUsername(bean.getUsername());
				bean.setSex(bean.getSex());
				putUserData(bean);
				finish();
			}else if(entity.getEventType() == MainPresenter.LoginFailureEvent){
				ByAlert.alert(entity.getData() + "");
			}
		}
	}
	
	private void putUserData(PersonDataBean bean){
		SPUtils.put(this, null, SpPublicParams.UID, bean.getUid());
		SPUtils.put(this, null, SpPublicParams.PHONE_MODEL, bean.getMobile());
		SPUtils.put(this, null, SpPublicParams.USERHEAD, bean.getHead());
		SPUtils.put(this, null, SpPublicParams.NICK_NAME, bean.getNickname());
		SPUtils.put(this, null, SpPublicParams.SEX, bean.getSex());
		SPUtils.put(this, null, SpPublicParams.USER_NAME, bean.getUsername());
		
		Intent intent=new Intent("changeavart");
		sendBroadcast(intent);
	}
}
